<template>
	<view class="wf-box" :style="{ paddingTop: totalheight }">
		<view class="head" :style="{ paddingTop: navBarHeight }">
			<view class="head-title">Chain Aibit</view>

			<view class="head-opt">

				<view class="head-add" @click.stop="expand">
					<image src="../../static/img/chat-02.png" class="head-icon"></image>
				</view>
			</view>
		</view>

		<!-- <view class="m-relative"> -->

			<view class="notice" @click="jumpNotice()">
				<view class="notice-left">
					<image src="../../static/img/notice.png"></image>
					<text>系统公告:</text>
				</view>

				<uni-notice-bar class="not-bar" scrollable single :text="noticeMsg" background-color="transparent"
					color="#666666" speed="5"></uni-notice-bar>
			</view>

			<view class="chat-main">
				<view class="chat-um" v-for="(chat,index) in chatStore.chats">
					<chat-item :chat="chat" :index="index" @longpress.native="onShowMenu($event,index)"></chat-item>
				</view>
			</view>

		<!-- </view> -->


		<view class="operate-list" v-if="moreShow" @click="closePopup">
			<!-- :style="{ top: height }" -->
			<view class="oper-mil" :style="{ top: totalheight }">
				<uni-transition ref="ani" custom-class="transition" mode-class="fade" :styles="styles" :show="moreFlag">
					<view class="operate-nbox">
						<view class="oper-im" @click="addFriends()">
							<view class="oper-im-bwarp">
								<image src="../../static/img/chat-04.png"></image>
								<text>添加好友</text>
							</view>
						</view>

						<view class="oper-im" @click="startGroup()">
							<view class="oper-im-bwarp">
								<image src="../../static/img/chat-05.png"></image>
								<text>发起群聊</text>
							</view>
						</view>

						<view class="oper-im" @click="sweep()">
							<view class="oper-im-bwarp">
								<image src="../../static/img/chat-06.png"></image>
								<text>扫一扫</text>
							</view>
						</view>
					</view>
				</uni-transition>
			</view>


		</view>

		<pop-menu v-show="menu.show" :menu-style="menu.style" :items="menu.items" @close="menu.show=false"
			@select="onSelectMenu"></pop-menu>
			
		<uni-popup ref="popup2" type="center">
			<view class="popup-box">
				<view class="popup-title">绑定推荐人</view>
				<input type="text" placeholder="请输入推荐人邀请码" v-model="bangID" />
				<view class="popup-footer">
					<view class="cancle" @click="close()">取消</view>
					<view class="confim" @click="confim()">确定</view>
				</view>
			</view>
		</uni-popup>

	</view>

</template>

<script>
	import navBarMixin from '@/mixin/navBarMixin.js'
	import { getMeUserAPI, noticeListApi, bindInviteUser } from '@/api/api.js'
	export default {
		data() {
			return {
				totalHeight: '',
				topheight: '',
				moreShow: false,
				moreFlag: false,
				noticeMsg: '',
				styles: {
					justifyContent: 'center',
					alignItems: 'center',
					width: '220rpx',
					textAlign: 'center',
					backgroundColor: 'transparent',
				},
				opmillHeight: '',

				menu: {
					show: false,
					style: "",
					chatIdx: -1,
					items: [{
							key: 'DELETE',
							name: '删除',
							icon: 'trash'
						},
						{
							key: 'TOP',
							name: '置顶',
							icon: 'arrow-up'
						}
					]
				},
				bangID: '',
			}
		},
		mixins: [navBarMixin],
		methods: {
			confim() {
				bindInviteUser({inviteCode:this.bangID}).then(res=>{
					this.close()
					if(res.code==0){
						uni.showToast({
							title:'绑定成功！',
							icon:'none'
						})
					}else {
						uni.showToast({
							title:res.message,
							icon:'none'
						})
					}
				})
			},
			openpup() {
				this.$refs.popup2.open()
			},
			close() {
				this.$refs.popup2.close()
			},
			expand() {
				this.moreShow = !this.moreShow
				this.moreFlag = !this.moreFlag
			},
			closePopup() {
				console.log('111')
				this.moreFlag = false
				this.moreShow = false
			},
			jumpNotice() {
				uni.navigateTo({
					url: '/pages/common/noticeList'
				})
			},

			addFriends() {
				uni.navigateTo({
					url: '/pages/friend/friend-add'
				})
			},
			startGroup() {
				uni.navigateTo({
					url: '/pages/group/group-edit?type=1'
				})
			},
			sweep() {
				console.log('扫一扫')
				uni.scanCode({
					scanType: ['qrCode'],
					// autoDecodeCharset: true,
					autoZoom: false,
					success: function (res) {
						uni.navigateTo({
							url: '/pages/friend/userInfo?data=' + res.result
						})
						// console.log('res===',res)
					}
				})
			},

			onSelectMenu(item) {
				switch (item.key) {
					case 'DELETE':
						this.removeChat(this.menu.chatIdx);
						break;
					case 'TOP':
						this.moveToTop(this.menu.chatIdx);
						break;
					default:
						break;
				}
				this.menu.show = false;
			},

			onShowMenu(e, chatIdx) {
				uni.getSystemInfo({
					success: (res) => {
						let touches = e.touches[0];
						let style = "";
						/* 因 非H5端不兼容 style 属性绑定 Object ，所以拼接字符 */
						if (touches.clientY > (res.windowHeight / 2)) {
							style = `bottom:${res.windowHeight-touches.clientY}px;`;
						} else {
							style = `top:${touches.clientY}px;`;
						}
						if (touches.clientX > (res.windowWidth / 2)) {
							style += `right:${res.windowWidth-touches.clientX}px;`;
						} else {
							style += `left:${touches.clientX}px;`;
						}
						this.menu.style = style;
						this.menu.chatIdx = chatIdx;
						// 
						this.$nextTick(() => {
							this.menu.show = true;
						});
					}
				})
			},

			removeChat(chatIdx) {
				this.$store.commit("removeChat", chatIdx);
			},

			moveToTop(chatIdx) {
				this.$store.commit("moveTop", chatIdx);
			},

			async getNoticeList() {
				const res = await noticeListApi()
				this.noticeMsg = '《' + res.data[0].noticeTitle + '》' + res.data[0].contentStandard
			},
			refreshUnreadBadge() {
				if (this.unreadCount > 0) {
					uni.setTabBarBadge({
						index: 0,
						text: this.unreadCount + ""
					})
				} else {
					uni.removeTabBarBadge({
						index: 0,
						complete: () => {}
					})
			
				}
			}
		},
		mounted() {
			if (uni.getStorageSync('token')) {
				getMeUserAPI().then(res=>{
					console.log(res.data.inviteUser);
					if(!res.data.inviteUser) {
						this.openpup()
					}
				})
			}
		},
		computed: {
			chatStore() {
				return this.$store.state.chatStore;
			},
			unreadCount() {
				let count = 0;
				this.chatStore.chats.forEach(chat => {
					count += chat.unreadCount;
				})
				return count;
			},
		},

		onLoad() {
			if (!uni.getStorageSync('token')) {
				uni.redirectTo({
					url: '/pages/login/start'
				})
			}
			// this.getMeUserInfo()
		},
		watch: {
			unreadCount(newCount, oldCount) {
				this.refreshUnreadBadge();
			}
		},

		onShow() {
			// 公告
			this.getNoticeList()
			// uni.hideTabBarRedDot({
			// 	index: 0
			// })
			this.refreshUnreadBadge();
		}

	}
</script>

<style lang="scss" scoped>
	page {
		background: #f8f8f8;
	}

	.wf-box {
		position: relative;
		min-height: 100vh;
		overflow: hidden;
		box-sizing: border-box;
	}

	.head {
		position: fixed;
		top: 0;
		z-index: 1;
		width: 100%;
		// height: 96rpx;
		background: #fff;
		height: 48px;

		.head-title {
			margin: 0 auto;
			font-size: 36rpx;
			font-weight: bold;
			line-height: 96rpx;
			text-align: center;
		}

		.head-opt {
			position: absolute;
			right: 30rpx;
			bottom: 28rpx;
			display: flex;
			align-items: center;

			.head-search {
				margin-right: 40rpx;
			}

			.head-icon {
				width: 40rpx;
				height: 40rpx;
				display: block;
			}

			.head-add {
				position: relative;

			}

		}
	}
	.m-relative {
		position: relative;
	}
	
	.notice {
		position: fixed;
		width: 100%;
		z-index: 99;
		// top: 96rpx;
		height: 80rpx;
		display: flex;
		align-items: center;
		padding: 0 30rpx;
		background: #F2F2F2;
		box-sizing: border-box;
		.notice-left {
			flex: 1;
			display: flex;
			align-items: center;
			justify-content: space-between;

			image {
				// margin-right: 20rpx;
				width: 48rpx;
				height: 48rpx;
			}

			text {
				color: #666666;
				font-size: 28rpx;
			}
		}

		.not-bar {
			width: 74%;
			margin-bottom: 0 !important;
		}
	}

	.chat-main {
		padding: 0 30rpx;
		padding-top: 80rpx;
		// margin-top: 96rpx;
		background: #f8f8f8;
		overflow: hidden;



		.chat-um {
			display: flex;
			align-items: center;

			::v-deep .chat-item {
				width: 100%;
				background: transparent;
			}

			// padding: 30rpx 0;
			.um-avatar {
				image {
					display: block;
					width: 80rpx;
					height: 80rpx;
				}
			}

			.um-info {
				flex: 1;
				margin-left: 20rpx;
				padding: 30rpx 0;
				border-bottom: 2rpx solid #eee;
				overflow: hidden;

				.info-nt {
					display: flex;

					.info-name {
						flex: 1;
						padding-right: 40rpx;
						font-size: 28rpx;
						color: #333;
					}

					.info-time {
						margin-left: auto;
						font-size: 24rpx;
						color: #999999;
					}
				}

				.info-new {
					margin-top: 8rpx;
					font-size: 24rpx;
					color: #999;
				}
			}
		}
	}


	.operate-list {
		position: absolute;
		top: 0;
		width: 100%;
		min-height: 100%;
		background: transparent;

		.oper-mil {
			position: fixed;
			z-index: 199;
			top: 96rpx;
			right: 26rpx;
			width: 220rpx;

			// height: 276rpx;
			// background: red;
			.operate-nbox {
				width: 100%;
				height: 100%;
				padding: 12rpx 0 0;
				background: url('../../static/img/chat-03.png') no-repeat;
				background-size: 100% 100%;
				box-sizing: border-box;

				.oper-im {
					padding: 0 30rpx;

					.oper-im-bwarp {
						padding: 15rpx 0;
						display: flex;
						// justify-content: space-between;
					}

					image {
						width: 48rpx;
						height: 48rpx;
					}

					text {
						margin-left: 16rpx;
						color: #fff;
						font-size: 24rpx;
					}
				}


			}
		}
	}
	
	.popup-box {
		width: 583rpx;
		height: 550rpx;
		border-radius: 20rpx;
		box-sizing: border-box;
		background: #fff;
		background: url('../../static/img/me-32.png') no-repeat;
		background-size: cover;
		overflow: hidden;
	
		.popup-title {
			font-size: 32rpx;
			color: #333;
			font-weight: bold;
			text-align: center;
			margin-top: 180rpx;
		}
		.popup-footer {
			display: flex;
			justify-content: space-between;
			margin: 72rpx 52rpx 0;
	
			.cancle {
				width: 200rpx;
				line-height: 76rpx;
				text-align: center;
				border: 2rpx solid #3F93FF;
				border-radius: 40rpx;
				color: #3F93FF;
			}
	
			.confim {
				width: 200rpx;
				line-height: 76rpx;
				text-align: center;
				border-radius: 40rpx;
				background: #3F93FF;
				color: #fff;
			}
		}
	
		input {
			// width: 500rpx;
			margin: 40rpx 40rpx 0;
			padding: 0 30rpx;
			height: 80rpx;
			font-size: 26rpx;
			line-height: 80rpx;
			background: #F4F4F4;
			border-radius: 6rpx 6rpx 6rpx 6rpx;
		}
	}
</style>